<?php
include_once("common.php");

$act = $_REQUEST['act'] ?: '';

if ($act == 'add') {
    //设置需要搜索的表格的前缀后缀
    $table = $table_Prefix . "event_over_comment";

    $user_insert['ai_article_id'] = $_REQUEST['event_nid'];
    $user_insert['ai_comment_user_id'] = $user_id;
    $user_insert['ai_parent_id'] = $_REQUEST['pId'];
    $user_insert['ai_content'] = $_REQUEST['content'];
    $user_insert['ai_reply_user_id'] = $_REQUEST['reply_user_id'];
    $user_insert['ai_create_time'] = date("Y-m-d H:i:s");

    // 记录@的用户位置
    $atMemberList = json_decode($_REQUEST['atMemberList'], true);
    $at_member_nid = '';
    $at_member_name = '';
    foreach ($atMemberList as $value) {
        $at_member_nid = $at_member_nid . $value['nid'] . ',';
        $at_member_name = $at_member_name . $value['nickname'] . ',';
    }
    $user_insert['ai_at_member_nid'] = substr($at_member_nid, 0, -1);
    $user_insert['ai_at_member_name'] = substr($at_member_name, 0, -1);


    $sql = insert_date($table, $user_insert);
    $query = do_query($sql) or die (mysqli_error() . $sql);
    if ($query) {

        // 通知 @的会员
        foreach ($atMemberList as $value) {
            $event_over_comment = select_lines_all_items('athlon_event_over_comment',' nid desc ',"article_id = ".$_REQUEST['event_nid']." AND comment_user_id = $user_id AND at_member_nid = '".$user_insert['ai_at_member_nid']."'");
            add_memeber_circle('at',$user_id,$event_over_comment[0]['nid'],$value['nid']);
        }

        $arr['status'] = 200;
        $arr['info'] = '';
        die(json_encode($arr));
    }


} else if ($act == 'list') {

    $table = $table_Prefix . "event_over_comment";
    $event_list = select_lines_all_items($table, 'nid asc', ' status = 0 AND article_id =' . $_REQUEST['event_nid']);
    $data = array();
    foreach ($event_list as $key => $value) {

        $user_info = select_one_line_all_items($table_Prefix . 'member', 'nid=' . $value['comment_user_id']);

        $data[$key]['id'] = $value['nid'];
        $data[$key]['parentId'] = $value['parent_id'];
        $data[$key]['content'] = $value['content'];
        $data[$key]['createTime'] = $value['create_time'];
        $data[$key]['avatarUrl'] = $user_info['headimgurl'];
        $data[$key]['nickName'] = $user_info['nickname'];
        $data[$key]['comment_user_id'] = $value['comment_user_id'];
        $data[$key]['owner'] = $value['comment_user_id'] == $user_id;

        $at_member_nid_list = empty($value['at_member_nid']) ? array() : explode(',', $value['at_member_nid']);

        $at_member_name_list = empty($value['at_member_name']) ? array() : explode(',', $value['at_member_name']);
        $content = $value['content'];
        $content_list = array();

        $emp_arr = array();
        foreach ($at_member_name_list as $nameKey => $nameValue) {

            $at_name = '@' . $nameValue;

            $position = stripos($content, $at_name);

            //如果 @不是第一位，把第一次出现@的位置之前的字符串切割存在数组的第一位
            if ($position > 0) {
                $emp_arr['content'] = substr($content, 0, $position);
                $emp_arr['member_nid'] = 0;
                $emp_arr['at'] = false;
                array_push($content_list, $emp_arr);
            }

            $emp_arr['content'] = substr($content, $position, strlen($at_name));
            $emp_arr['member_nid'] = $at_member_nid_list[$nameKey];
            $emp_arr['at'] = true;
            array_push($content_list, $emp_arr);

            $content = substr($content, $position + strlen($at_name), strlen($content));
        }
        //剩下的字符存在数组的末尾
        $emp_arr['content'] = $content;
        $emp_arr['member_nid'] = 0;
        $emp_arr['at'] = false;
        array_push($content_list, $emp_arr);

        //回复用户
        if($value['reply_user_id'] > 0){
            $reply_member = select_one_line_all_items($table_Prefix . 'member', 'nid=' . $value['reply_user_id']);
            $temp_reply_member = array();
            $temp_reply_member['nid'] = $reply_member['nid'];
            $temp_reply_member['name'] = $reply_member['nickname'];
            $data[$key]['reply_user'] = $temp_reply_member;
        }

        $data[$key]['content_list'] = $content_list;
    }

    $re_data['readNumer'] = count_by_item($table, ' status = 0 AND article_id =' . $_REQUEST['event_nid']);
    $re_data['commentList'] = $data;
    $arr['status'] = 200;
    $arr['data'] = $re_data;
    die(json_encode($arr));
} else if ($act == 'del') {

    $table = $table_Prefix . "event_over_comment";
    $id = $_REQUEST['commentId'];
    $deleteSql = "UPDATE $table SET status = 3 WHERE nid = $id or parent_id = $id ";
    $query = do_query($deleteSql) or die (mysqli_error() . $deleteSql);

    if ($query) {
        $arr['status'] = 200;
        $arr['info'] = '删除成功';
        die(json_encode($arr));
    } else {
        $arr['status'] = 301;
        $arr['info'] = '删除失败';
        die(json_encode($arr));
    }

}


